<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-18 18:16:27;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */

namespace modules\sys\service\impl;


use application\core\Lib;
use application\core\Request;
use application\core\Result;
use modules\sys\service\LogsService;

class LogsServiceImpl implements LogsService
{

    function lists(): array
    {
        $fileName = Request::json()['file_name'];
        if (!empty($fileName)) {
            if (strpos($fileName, "../") || strpos($fileName, "./")) {
                return Result::fail(false, ["data" => []], "参数不合法");
            }
        }
        $dir = APP_PATH . "resources/data/logs/";
        if (!empty($fileName)) {
            $dir = APP_PATH . "resources/data/logs/" . $fileName;
        }
        $lists = Lib::scanDirByUpdateDate($dir, true);

        $res = [];
        foreach ($lists as $k => $file) {
            if (is_dir($dir . $file)) {
                $res[$k]['file_type'] = "<a style='color:dodgerblue'>目录</a>";
                $res[$k]['file_size'] = "0";

                $res[$k]['file_name'] = "<a style='color:dodgerblue' onclick='openLogsDir(\"" . $fileName . $file . "/\")'>" . $fileName . $file . "</a>";
            } else {
                $res[$k]['file_type'] = "<a style='color:green'>文件</a>";
                $res[$k]['file_size'] = Lib::fmtMoney(filesize($dir . $file)/1024)."kb";
                $res[$k]['file_name'] = "<a style='color:green' onclick='openLogsFile(\"" . $fileName . $file . "\")'>" . $fileName . $file . "</a>";
            }
            $res[$k]['ori_file_name'] = $fileName . $file;

            $res[$k]['last_update'] = date("Y-m-d H:i:s", filemtime($dir));
        }
        return Result::success(false, ["rows" => $res]);
    }

    public function show(): array
    {
        $fileName = Request::json()['file_name'];

        $dir = APP_PATH . "resources/data/logs/" . $fileName;
        if (!file_exists($dir)) {
            return Result::fail(false, [], "文件不存在");
        }
        $content = file_get_contents($dir);
        return Result::success(false, "$content");

    }

    public function delLogs(): array
    {
        $fileNames = Request::json()['file_names'];
        $fileNamesArr = explode(",", $fileNames);
        foreach ($fileNamesArr as $file) {
            if (strpos($file, "../") || strpos($file, "./")) {
                return Result::fail(false, ["data" => []], "参数不合法");
            }
            $file = APP_PATH . "resources/data/logs/" . $file;
            if (file_exists($file)) {
                if (!is_dir($file)) {
                    unlink($file);
                } else {
                    Lib::delAllByDir($file);
                }
            }
        }
        return Result::success(false, "", "操作成功");
    }

}